package influence.propagation.swing.implementation;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class GraphFileChooser extends JFrame {
	
	private class ChooseEvent implements ActionListener{
		
		private boolean isGraphFile = false;
		
		public ChooseEvent(boolean isGraphFile) {
			this.isGraphFile=isGraphFile;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			JFileChooser fc = new JFileChooser("C:\\Users\\Valerio\\Desktop");
			int action = fc.showOpenDialog(ref);
			if (action == JFileChooser.APPROVE_OPTION) {
				File f = fc.getSelectedFile();
				if (isGraphFile) {
					label_file_1.setText(f.getAbsolutePath());
				} else {
					label_file_2.setText(f.getAbsolutePath());
				}
			}
		}
	}


	private static final long serialVersionUID = -4783527352691872615L;
	private JPanel contentPane;
	private JLabel lblFilegraph;
	private JButton btnSfoglia_2_1;
	private JLabel label_file_1;
	private JLabel lblFiletxt;
	private JLabel label_file_2;
	private JButton btnSfoglia_2;
	private JButton btnOk;
	private JButton btnAnnulla;
	private GraphFileChooser ref;
	private InfluencePropagation parent;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GraphFileChooser frame = new GraphFileChooser();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public void setParent(InfluencePropagation parent){
		this.parent=parent;
		parent.setEnabled(false);
	}

	/**
	 * Create the frame.
	 */
	public GraphFileChooser() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent arg0) {
				parent.setEnabled(true);
				ref.dispose();
			}
		});
		setTitle("Scegli i file");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 189);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLblFilegraph());
		contentPane.add(getLabel_file_1());
		contentPane.add(getBtnSfoglia_2_1());
		contentPane.add(getLblFiletxt());
		contentPane.add(getLabel_file_2());
		contentPane.add(getBtnSfoglia_2());
		contentPane.add(getBtnOk());
		contentPane.add(getBtnAnnulla());
		ref=this;
	}

	private JLabel getLblFilegraph() {
		if (lblFilegraph == null) {
			lblFilegraph = new JLabel("File .graph:");
			lblFilegraph.setBounds(13, 35, 67, 14);
		}
		return lblFilegraph;
	}
	private JButton getBtnSfoglia_2_1() {
		if (btnSfoglia_2_1 == null) {
			btnSfoglia_2_1 = new JButton("Sfoglia");
			btnSfoglia_2_1.addActionListener(new ChooseEvent(true));
			btnSfoglia_2_1.setBounds(307, 31, 76, 23);
		}
		return btnSfoglia_2_1;
	}
	private JLabel getLabel_file_1() {
		if (label_file_1 == null) {
			label_file_1 = new JLabel("\"\"");
			label_file_1.setBounds(92, 35, 203, 14);
		}
		return label_file_1;
	}
	private JLabel getLblFiletxt() {
		if (lblFiletxt == null) {
			lblFiletxt = new JLabel("File .txt:");
			lblFiletxt.setBounds(13, 69, 67, 14);
			lblFiletxt.setAlignmentY(Component.TOP_ALIGNMENT);
			lblFiletxt.setAlignmentX(Component.CENTER_ALIGNMENT);
		}
		return lblFiletxt;
	}
	private JLabel getLabel_file_2() {
		if (label_file_2 == null) {
			label_file_2 = new JLabel("\"\"");
			label_file_2.setBounds(92, 69, 203, 14);
		}
		return label_file_2;
	}
	private JButton getBtnSfoglia_2() {
		if (btnSfoglia_2 == null) {
			btnSfoglia_2 = new JButton("Sfoglia");
			btnSfoglia_2.addActionListener(new ChooseEvent(false));
			btnSfoglia_2.setBounds(307, 65, 76, 23);
		}
		return btnSfoglia_2;
	}
	private JButton getBtnOk() {
		if (btnOk == null) {
			btnOk = new JButton("Ok");
			btnOk.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
						File f1 = new File(label_file_1.getText());
						File f2 = new File(label_file_2.getText());
						parent.instanziateController(f1, f2);
						parent.setEnabled(true);
						ref.dispose();
				}
			});
			btnOk.setBounds(78, 127, 89, 23);
		}
		return btnOk;
	}
	private JButton getBtnAnnulla() {
		if (btnAnnulla == null) {
			btnAnnulla = new JButton("Annulla");
			btnAnnulla.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					parent.setEnabled(true);
					ref.dispose();
				}
			});
			btnAnnulla.setBounds(265, 127, 89, 23);
		}
		return btnAnnulla;
	}
}
